﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDE_FIC.DAO;
using SDE_FIC.Models;
using PagedList;

namespace SDE_FIC.Controllers
{
    public class AlunoController : Controller
    {
        private DBConnect _db = new DBConnect();

        //
        // GET: /Aluno/

        public ActionResult Index(int? pagina)
        {
            int tamanhoPagina = 10;
            int numeroPagina = pagina ?? 1;

            AlunoDAO alunoDAO = new AlunoDAO(ref _db);
            return View("~/Views/Sistema/Aluno/Index.cshtml", alunoDAO.All().ToPagedList(numeroPagina, tamanhoPagina));
        }

        //
        // GET: /Aluno/Details/5

        public ActionResult Details(int Id = 0)
        {

            AlunoDAO alunoDAO = new AlunoDAO(ref _db);
            Aluno _aluno = new Aluno();
            _aluno = alunoDAO.LoadById(Id);
            
            
            if (_aluno == null)
            {
                return HttpNotFound();
            }
            return View("~/Views/Sistema/Aluno/Details.cshtml", _aluno);
        }


        //
        // GET: /Aluno/Create

        public ActionResult Create()
        {
            return View("~/Views/Sistema/Aluno/Create.cshtml");
        }

        //
        // POST: /Aluno/Create

        [HttpPost]
        public ActionResult Create(Aluno aluno)
        {

            if (ModelState.IsValid)
            {
                AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
                _alunoDAO.Insert(aluno);
                return RedirectToAction("Index");
            }

            return View("~/Views/Sistema/Aluno/Create.cshtml");
        }

        //
        // GET: /Aluno/Edit/5

        public ActionResult Edit(int id = 0)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            Aluno _aluno = new Aluno();

            _aluno = _alunoDAO.LoadById(id);

            if (_aluno == null)
            {
                return HttpNotFound();
            }
            return View("~/Views/Sistema/Aluno/Edit.cshtml", _aluno);
        }

        //
        // POST: /Aluno/Edit/5

        [HttpPost]
        public ActionResult Edit(Aluno aluno)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            Aluno _aluno = new Aluno();

            if (ModelState.IsValid)
            {
                _alunoDAO.Update(aluno);
                return RedirectToAction("Index");
            }

            return View("~/Views/Sistema/Aluno/Edit.cshtml", _aluno);
        }

        //
        // GET: /Aluno/Delete/5

        public ActionResult Delete(int id = 0)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            Aluno _aluno = new Aluno();

            _aluno = _alunoDAO.LoadById(id);

            if (_aluno == null)
            {
                return HttpNotFound();
            }
            return View("~/Views/Sistema/Aluno/Delete.cshtml", _aluno);
        }

        //
        // POST: /Aluno/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Aluno aluno)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            Aluno _aluno = new Aluno();

            if (ModelState.IsValid)
            {
                _alunoDAO.Delete(aluno);
                return RedirectToAction("Index");
            }

            return View("~/Views/Sistema/Aluno/Delete.cshtml", _aluno);
        }

        //protected override void Dispose(bool disposing)
        //{
        //  //  db.Dispose();
        //    base.Dispose(disposing);
        //}
    }
}